import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {Button, Form, Icon, message, Modal, Upload,} from 'antd';
// import { sub } from '../../sub';

@connect(({usermanager, loading}) => ({
  usermanager,
  loading: loading.models.usermanager,
}))
@Form.create()
class UploadFileForm extends PureComponent {
  state = {
    fileList: [],
    uploading: false,
  }
  doUpload = () => {
    const {dispatch,isUser,handleUploadVisible,pageList,addParmas} = this.props;
    const {fileList} = this.state;
    const formData = new FormData();
    fileList.forEach((file) => {
      formData.append('file', file);
    });
    // formData.append('dirId', 0);
    if (isUser) {
      formData.append('orgId', addParmas.id);
    }
    // formData.append('dirId', currentDir.id || 0);
    this.setState({
      uploading: true,
    });

    let url = '';
    if(isUser){  //用户管理页面
      url = 'usermanager/importData';
    }else{  //弱密码管理页面
      url = 'usermanager/weakpwdImport';
    }


    dispatch({
      type:url ,
      payload: formData,
      callback: (res) => {
        if (res.result == "ok") {
          this.setState({
            fileList: [],
            uploading: false,
          });
          Modal.success({title: res.msg});
          pageList();
          handleUploadVisible();

        } else {
          Modal.error({title: '操作失败', content: res.msg});
          this.setState({
            uploading: false,
          });
        }
      },
    });


  }
  downloadTemplate = () => {
    const { isUser } = this.props;
    if(isUser){
      console.log("模板下载")
    }else{
      window.open(location.origin+"/dsrs/s/weakpwd/download","_top");
    }
    
    //  const {dispatch} = this.props;
    //  dispatch({
    //   type:'usermanager/weakpwdDownloadt' ,
    //   callback: (res) => {
    //     if (res.result == "ok") {
    //       // window.open(sub+"/用户信息表.xlsx","_top");

    //     } else {
    //       message.error(res.msg);
    //     }
    //   },
    // });
    
  }

  render() {
    const {uploadVisible, form, handleUploadVisible, values} = this.props;
    const {uploading, fileList} = this.state;


    const props = {
      onRemove: (file) => {
        this.setState((state) => {
          const index = state.fileList.indexOf(file);
          const newFileList = state.fileList.slice();
          newFileList.splice(index, 1);
          return {
            fileList: newFileList,
          };
        });
      },
      beforeUpload: (file) => {
        this.setState(state => ({
          fileList: [...state.fileList, file],
        }));
        return false;
      },
      fileList,
    };

    const okHandle = () => {
      form.validateFields((err, fieldsValue) => {
        if (err) return;
        form.resetFields();
      });
    };

    return (
      <Modal
        destroyOnClose
        title='上传文件'
        visible={uploadVisible}
        footer={[
            <Button key="sure" type="primary" onClick={() => handleUploadVisible()}>确定</Button>            
        ]}
        onOk={okHandle}
        onCancel={() => handleUploadVisible()}
      >
        <div>
          <div style={{marginBottom:'15px'}}>
            <Button key="down" onClick={() => this.downloadTemplate()}>
               <Icon type="arrow-down" /> 下载模板
            </Button>
          </div>          
          <Upload {...props}>
            <Button key="file">
              <Icon type="upload"/> 选择文件
            </Button>
          </Upload>
          <Button
            key="upload"
            type="primary"
            onClick={this.doUpload}
            disabled={fileList.length === 0}
            loading={uploading}
            style={{marginTop: 16}}
          >
            {uploading ? '上传中' : '开始上传'}
          </Button>
        </div>
      </Modal>


    );
  }
}

export default UploadFileForm;
